﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;

namespace Component
{
    public class ProviderXML
    {

        #region ===============Function common=====================
        public static bool Detele(int id, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (node.FirstChild.InnerText == id.ToString())
                        xmlDoc.DocumentElement.RemoveChild(node);
                }
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }
        private static void CreateElement(XmlDocument xmlDoc, XmlElement parent, string tagName, string innerText)
        {
            XmlElement newEle = xmlDoc.CreateElement(tagName);
            newEle.InnerText = innerText;
            parent.AppendChild(newEle);
        }
        private static void CreateAttribute(XmlDocument xmlDoc, XmlNode node, string attName, string innerText)
        {
            XmlAttribute newAtt = xmlDoc.CreateAttribute(attName);
            newAtt.InnerText = innerText;
            node.Attributes.Append(newAtt);
        }
        private static XmlNode FindNodeByName(XmlNode parent, string name)
        {
            foreach (XmlNode node in parent)
            {
                if (node.Name == name)
                    return node;
            }
            return null;
        }

        private static void UpdateImage(XmlDocument xmlDoc, List<Images> list, XmlNode child)
        {
            child.RemoveAll();
            if (list != null)
            {

                int id = 1;
                foreach (Images img in list)
                {
                    XmlElement image = xmlDoc.CreateElement("Image");
                    CreateAttribute(xmlDoc, image, AttributeImage._Id, id.ToString());
                    id++;
                    CreateAttribute(xmlDoc, image, AttributeImage._LargeSource, img.LargeSource);
                    CreateAttribute(xmlDoc, image, AttributeImage._SmallSource, img.SmallSource);
                    CreateAttribute(xmlDoc, image, AttributeImage._LabelEl, img.LabelEl);
                    CreateAttribute(xmlDoc, image, AttributeImage._LabelVn, img.LabelVn);
                    CreateAttribute(xmlDoc, image, AttributeImage._Priority, img.Priority.ToString());
                    child.AppendChild(image);
                }
            }
        }

        private static void UpdateFiles(XmlDocument xmlDoc, List<Files> list, XmlNode child)
        {
            child.RemoveAll();
            if (list != null)
            {

                foreach (Files file in list)
                {
                    XmlElement image = xmlDoc.CreateElement("File");
                    CreateAttribute(xmlDoc, image, ColumnFiles._SourceEl, file.SourceEL);
                    CreateAttribute(xmlDoc, image, ColumnFiles._SourceVn, file.SourceVN);
                    child.AppendChild(image);
                }
            }
        }


        #endregion

        #region =================Category Provider==================
      
        public static List<CategoryInfo> GetAll_Category(string pathFileXMl)
        {
            try
            {
                List<CategoryInfo> result = new List<CategoryInfo>();
                CategoryInfo catInfo;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXMl);
                XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    catInfo = new CategoryInfo();
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.NodeType == XmlNodeType.Element)
                        {
                            switch (childNode.Name)
                            {
                                case ColumCategory._Id:
                                    catInfo.Id = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumCategory._Summary:
                                    catInfo.Summary = childNode.InnerText;
                                    break;
                                case ColumCategory._Name:
                                    catInfo.Name = childNode.InnerText;
                                    break;
                                case ColumCategory._Content:
                                    catInfo.Content = childNode.InnerText;
                                    break;
                                case ColumCategory._UrlAdmin:
                                    catInfo.UrlAdmin = childNode.InnerText;
                                    break;
                                case ColumCategory._UrlFronent:
                                    catInfo.UrlFronent = childNode.InnerText;
                                    break;
                                case ColumCategory._Parent:
                                    catInfo.Parent = ConvertType.ToInt(childNode.InnerText);
                                    break;
                                case ColumCategory._Priority:
                                    catInfo.Priority = ConvertType.ToInt(childNode.InnerText);
                                    break;                                
                            }

                        }

                    }
                    result.Add(catInfo);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        public static CategoryInfo GetCategoryById(int id, List<CategoryInfo> list)
        {
            return list.Single(cat => cat.Id == id);
        }

        public static bool Insert_Category(CategoryInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
                info.Id = lastID + 1;
                //info._Parent += info.Id.ToString();
                XmlElement newElement = xmlDoc.CreateElement("Category");

                CreateElement(xmlDoc, newElement, ColumCategory._Id,ConvertType.ToString(info.Id));
                CreateElement(xmlDoc, newElement, ColumCategory._Content, ConvertType.ToString(info.Content));
                CreateElement(xmlDoc, newElement, ColumCategory._Name, ConvertType.ToString(info.Name));
                CreateElement(xmlDoc, newElement, ColumCategory._Parent, ConvertType.ToString(info.Parent));
                CreateElement(xmlDoc, newElement, ColumCategory._Priority, ConvertType.ToString(info.Priority));
                CreateElement(xmlDoc, newElement, ColumCategory._Summary, ConvertType.ToString(info.Summary));
                CreateElement(xmlDoc, newElement, ColumCategory._UrlAdmin, ConvertType.ToString(info.UrlAdmin));
                CreateElement(xmlDoc, newElement, ColumCategory._UrlFronent, ConvertType.ToString(info.UrlFronent));                
                xmlDoc.DocumentElement.AppendChild(newElement);
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// public static bool Update_Category(CategoryInfo info, string pathFileXML)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pathFileXML">Đường dẫn file XML</param>
        /// <returns></returns>
        public static bool Update_Category(CategoryInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (node.FirstChild.InnerText == info.Id.ToString())
                    {
                        foreach (XmlNode child in node.ChildNodes)
                        {

                            if (child.NodeType == XmlNodeType.Element)
                            {
                                switch (child.Name)
                                {
                                    case ColumCategory._Id:
                                        child.InnerText = info.Id.ToString();
                                        break;
                                    case ColumCategory._Summary:
                                        child.InnerText = info.Summary;
                                        break;
                                    case ColumCategory._Name:
                                        child.InnerText = info.Name;
                                        break;
                                    case ColumCategory._Content:
                                        child.InnerText = info.Content;
                                        break;
                                    case ColumCategory._UrlAdmin:
                                        child.InnerText = info.UrlAdmin;
                                        break;
                                    case ColumCategory._UrlFronent:
                                        child.InnerText = info.UrlFronent;
                                        break;
                                    case ColumCategory._Parent:
                                        child.InnerText = info.Parent.ToString();
                                        break;
                                    case ColumCategory._Priority:
                                        child.InnerText = info.Priority.ToString();
                                        break;   
                                }
                            }
                        }
                    }

                }
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }


        #endregion

        #region ===============News Provider================
        /// <summary>
        ///  public static List<NewsInfo> GetAll_News(string pathFileXMl)
        /// </summary>
        /// <param name="pathFileXMl">Đường dẫn file XML </param>
        /// <returns></returns>
        public static List<NewsInfo> GetAll_News(string pathFileXMl)
        {
            try
            {
                List<NewsInfo> result = new List<NewsInfo>();
                NewsInfo newInfo;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXMl);
                XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    newInfo = new NewsInfo();
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.NodeType == XmlNodeType.Element)
                        {
                            switch (childNode.Name)
                            {
                                case ColumNews._Id:
                                    newInfo.Id = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumNews._Active:
                                    newInfo.Active = childNode.InnerText;
                                    break;
                                case ColumNews._Content:
                                    newInfo.Content = childNode.InnerText;
                                    break;
                                case ColumNews._Img:
                                    newInfo.Img = childNode.InnerText;
                                    break;
                                case ColumNews._News:
                                    newInfo.News = childNode.InnerText;
                                    break;
                                case ColumNews._Parent:
                                    newInfo.Parent = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumNews._Date:
                                    try
                                    {
                                        newInfo.Date = Convert.ToDateTime(childNode.InnerText);
                                    }
                                    catch
                                    {
                                        
                                    }

                                    break;
                                case ColumNews._Summary:
                                    newInfo.Summary = childNode.InnerText;
                                    break;
                                case ColumNews._Titile:
                                    newInfo.Titile = childNode.InnerText;
                                    break;
                                case ColumNews._UrlDetailBackend:
                                    newInfo.UrlDetailBackend = childNode.InnerText;
                                    break;
                                case ColumNews._UrlDetailFronend:
                                    newInfo.UrlDetailFronend = childNode.InnerText;
                                    break;
                            }

                        }

                    }
                    result.Add(newInfo);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        ///  public static NewsInfo GetNewsById(int id, List<NewsInfo> list)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pathFileXML">Đường dẫn file XML </param>
        /// <returns></returns>
        public static NewsInfo GetNewsById(int id, List<NewsInfo> list)
        {
            return list.Single(cat => cat.Id == id);
        }

        /// <summary>
        ///  public static bool Insert_News(NewsInfo info, string pathFileXML)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pathFileXML">Đường dẫn file xml để insert</param>
        /// <returns></returns>
        public static bool Insert_News(NewsInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
                info.Id = lastID + 1;
                XmlElement newElement = xmlDoc.CreateElement("News");

                CreateElement(xmlDoc, newElement, ColumNews._Id, ConvertType.ToString(info.Id));
                CreateElement(xmlDoc, newElement, ColumNews._Active, ConvertType.ToString(info.Active));
                CreateElement(xmlDoc, newElement, ColumNews._Content, ConvertType.ToString(info.Content));
                CreateElement(xmlDoc, newElement, ColumNews._Img, ConvertType.ToString(info.Img));
                CreateElement(xmlDoc, newElement, ColumNews._Date, ConvertType.ToString(info.Date.ToShortDateString()));
                CreateElement(xmlDoc, newElement, ColumNews._News, ConvertType.ToString(info.News));
                CreateElement(xmlDoc, newElement, ColumNews._Parent, ConvertType.ToString(info.Parent));
                CreateElement(xmlDoc, newElement, ColumNews._Summary, ConvertType.ToString(info.Summary));
                CreateElement(xmlDoc, newElement, ColumNews._Titile, ConvertType.ToString(info.Titile));
                CreateElement(xmlDoc, newElement, ColumNews._UrlDetailBackend, ConvertType.ToString(info.UrlDetailBackend));
                CreateElement(xmlDoc, newElement, ColumNews._UrlDetailFronend, ConvertType.ToString(info.UrlDetailFronend));
                //Luu file xml
                xmlDoc.DocumentElement.AppendChild(newElement);
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// public static bool Update_News(NewsInfo info, string pathFileXML)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pathFileXML">Đường dẫn file XML</param>
        /// <returns></returns>
        public static bool Update_News(NewsInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (node.FirstChild.InnerText == info.Id.ToString())
                    {
                        foreach (XmlNode child in node.ChildNodes)
                        {

                            if (child.NodeType == XmlNodeType.Element)
                            {
                                switch (child.Name)
                                {
                                    case ColumNews._Id:
                                        child.InnerText = info.Id.ToString();
                                        break;
                                    case ColumNews._Active:
                                        child.InnerText = info.Active.ToString();
                                        break;
                                    case ColumNews._Content:
                                        child.InnerText = info.Content.ToString();
                                        break;
                                    case ColumNews._Img:
                                        child.InnerText = info.Img.ToString();
                                        break;
                                    case ColumNews._News:
                                        child.InnerText = info.News.ToString();
                                        break;
                                    case ColumNews._Parent:
                                        child.InnerText = info.Parent.ToString();
                                        break;
                                    case ColumNews._Date:
                                        try
                                        {
                                            child.InnerText = info.Date.ToString();
                                        }
                                        catch
                                        {
                                            
                                        }

                                        break;
                                    case ColumNews._Summary:
                                        child.InnerText = info.Summary;
                                        break;
                                    case ColumNews._Titile:
                                        child.InnerText = info.Titile;
                                        break;
                                    case ColumNews._UrlDetailBackend:
                                        child.InnerText = info.UrlDetailBackend;
                                        break;
                                    case ColumNews._UrlDetailFronend:
                                        child.InnerText = info.UrlDetailFronend;
                                        break;
                                }
                            }
                        }
                    }

                }
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region =================Users Provider==================
        /// <summary>
        /// public static List<UsersInfo> GetAll_Users(string pathFileXMl)
        /// </summary>
        /// <param name="pathFileXMl">Đường dẫn file XML </param>
        /// <returns></returns>
        public static List<UsersInfo> GetAll_Users(string pathFileXMl)
        {
            try
            {
                List<UsersInfo> result = new List<UsersInfo>();
                UsersInfo newInfo;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXMl);
                XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    newInfo = new UsersInfo();
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.NodeType == XmlNodeType.Element)
                        {
                            switch (childNode.Name)
                            {
                                case ColumUsers._Id:
                                    newInfo.Id = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumUsers._Active:
                                    newInfo.Active = childNode.InnerText;
                                    break;
                                case ColumUsers._Mail:
                                    newInfo.Mail = childNode.InnerText;
                                    break;
                                case ColumUsers._Datelogin:
                                    try
                                    {
                                        newInfo.Datelogin = Convert.ToDateTime(childNode.InnerText);
                                    }
                                    catch
                                    {
                                    }
                                    break;
                                case ColumUsers._Datecreated:
                                    try
                                    {
                                        newInfo.Datecreated = Convert.ToDateTime(childNode.InnerText);
                                    }
                                    catch
                                    {
                                    }
                                    break;
                                case ColumUsers._Name:
                                    newInfo.Name = childNode.InnerText;
                                    break;
                                case ColumUsers._Pass:
                                    newInfo.Pass = childNode.InnerText;
                                    break;
                                case ColumUsers._Phone:
                                    newInfo.Phone = childNode.InnerText;
                                    break;
                                case ColumUsers._User_name:
                                    newInfo.User_name = childNode.InnerText;
                                    break;                                
                            }
                        }
                    }
                    result.Add(newInfo);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        ///  public static bool Insert_Users(UsersInfo info, string pathFileXML)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pathFileXML">Đường dẫn file xml để insert</param>
        /// <returns></returns>
        public static bool Insert_Users(UsersInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
                info.Id = lastID + 1;
                XmlElement newElement = xmlDoc.CreateElement("Users");

                //ID
                CreateElement(xmlDoc, newElement, ColumUsers._Id, info.Id.ToString());
                CreateElement(xmlDoc, newElement, ColumUsers._Active, info.Active);
                CreateElement(xmlDoc, newElement, ColumUsers._Datelogin, info.Datelogin.ToShortDateString());
                CreateElement(xmlDoc, newElement, ColumUsers._Datecreated, info.Datecreated.ToShortDateString());
                CreateElement(xmlDoc, newElement, ColumUsers._Mail, info.Mail.ToString());
                CreateElement(xmlDoc, newElement, ColumUsers._Name, info.Name);
                CreateElement(xmlDoc, newElement, ColumUsers._Pass, info.Pass);
                CreateElement(xmlDoc, newElement, ColumUsers._Phone, info.Phone);
                CreateElement(xmlDoc, newElement, ColumUsers._User_name, info.User_name);


                //Luu file xml
                xmlDoc.DocumentElement.AppendChild(newElement);
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///  public static UsersInfo CheckUserLogin(string userName, string password, string pathFileXML)
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="pathFileXML"></param>
        /// <returns></returns>
        public static UsersInfo CheckUserLogin(string userName, string password, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    XmlNode usName = FindNodeByName(node, ColumUsers._User_name);
                    XmlNode pass = FindNodeByName(node, ColumUsers._Pass);
                    if (usName.InnerText == userName && pass.InnerText == password)
                        return GetByXMLNode(node);
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        ///  private static UsersInfo GetByXMLNode(XmlNode node)
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static UsersInfo GetByXMLNode(XmlNode node)
        {
            UsersInfo info = new UsersInfo();
            foreach (XmlNode item in node.ChildNodes)
            {
                if (item.NodeType == XmlNodeType.Element)
                {
                    switch (item.Name)
                    {
                        case ColumUsers._Id:
                            info.Id = Convert.ToInt32(item.InnerText);
                            break;
                        case ColumUsers._Active:
                            info.Active = item.InnerText;
                            break;
                        case ColumUsers._Datelogin:
                            try
                            {
                                info.Datelogin = Convert.ToDateTime(item.InnerText);
                            }
                            catch
                            {
                            }
                            break;
                        case ColumUsers._Datecreated:
                            try
                            {
                                info.Datecreated = Convert.ToDateTime(item.InnerText);
                            }
                            catch
                            {
                            }
                            break;
                        case ColumUsers._Mail:
                            info.Mail = item.InnerText;
                            break;
                        case ColumUsers._Name:
                            info.Name = item.InnerText;
                            break;
                        case ColumUsers._Pass:
                            info.Pass = item.InnerText;
                            break;
                        case ColumUsers._Phone:
                            info.Phone = item.InnerText;
                            break;
                        case ColumUsers._User_name:
                            info.User_name = item.InnerText;
                            break;
                    }

                }


            }
            return info;
        }
        /// <summary>
        ///  internal static bool Update_Users(UsersInfo info, string pathFileXML)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pathFileXML"></param>
        /// <returns></returns>

        internal static bool Update_Users(UsersInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (node.FirstChild.InnerText == info.Id.ToString())
                    {
                        foreach (XmlNode child in node.ChildNodes)
                        {

                            if (child.NodeType == XmlNodeType.Element)
                            {
                                switch (child.Name)
                                {
                                    case ColumUsers._User_name:
                                        child.InnerText = info.User_name;
                                        break;
                                    case ColumUsers._Pass:
                                        child.InnerText = info.Pass;
                                        break;
                                }
                            }
                        }
                    }
                }
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region ===============LienHe Provider================
        public static List<LienHeInfo> GetAll_LienHe(string pathFileXMl)
        {
            try
            {
                List<LienHeInfo> result = new List<LienHeInfo>();
                LienHeInfo lienheInfo;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXMl);
                XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    lienheInfo = new LienHeInfo();
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.NodeType == XmlNodeType.Element)
                        {
                            switch (childNode.Name)
                            {
                                case ColumLienHe._Id:
                                    lienheInfo.Id = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumLienHe._Adress:
                                    lienheInfo.Adress = childNode.InnerText;
                                    break;
                                case ColumLienHe._Content:
                                    lienheInfo.Content = childNode.InnerText;
                                    break;
                                case ColumLienHe._Email:
                                    lienheInfo.Email = childNode.InnerText;
                                    break;
                                case ColumLienHe._Name:
                                    lienheInfo.Name = childNode.InnerText;
                                    break;
                                case ColumLienHe._Phone:
                                    lienheInfo.Phone = childNode.InnerText;
                                    break;
                                case ColumLienHe._PL:
                                    lienheInfo.PL = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumLienHe._Title:
                                    lienheInfo.Title = childNode.InnerText;
                                    break;
                                case ColumLienHe._UrlAdmin:
                                    lienheInfo.UrlAdmin = childNode.InnerText;
                                    break;
                                case ColumLienHe._UrlFronent:
                                    lienheInfo.UrlFronent = childNode.InnerText;
                                    break;
                            }

                        }

                    }
                    result.Add(lienheInfo);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        public static LienHeInfo GetLienHeById(int id, List<LienHeInfo> list)
        {
            return list.Single(cat => cat.Id == id);
        }
        public static bool Insert_LienHe(LienHeInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                if(xmlDoc.DocumentElement.LastChild==null)
                {
                    info.Id = 1;
                }

                if (xmlDoc.DocumentElement.LastChild != null)
                {
                    int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
                    info.Id = lastID + 1;
                }
                XmlElement LienHeElement = xmlDoc.CreateElement("LienHe");

                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Id, info.Id.ToString());
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Adress, info.Adress.ToString());
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Content, info.Content.ToString());
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Email, info.Email.ToString());
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Name, info.Name);
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Phone, info.Phone.ToString());
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._PL, info.PL.ToString());
                CreateElement(xmlDoc, LienHeElement, ColumLienHe._Title, info.Title.ToString());
                //CreateElement(xmlDoc, LienHeElement, ColumLienHe._UrlAdmin, info.UrlAdmin.ToString());
                //CreateElement(xmlDoc, LienHeElement, ColumLienHe._UrlFronent, info.UrlFronent.ToString());
                xmlDoc.DocumentElement.AppendChild(LienHeElement);
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }

       
        public static bool Update_LienHe(LienHeInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (node.FirstChild.InnerText == info.Id.ToString())
                    {
                        foreach (XmlNode child in node.ChildNodes)
                        {

                            if (child.NodeType == XmlNodeType.Element)
                            {
                                switch (child.Name)
                                {
                                    case ColumLienHe._Id:
                                        child.InnerText = info.Id.ToString();
                                        break;
                                    case ColumLienHe._Adress:
                                        child.InnerText = info.Adress.ToString();
                                        break;
                                    case ColumLienHe._Content:
                                        child.InnerText = info.Content.ToString();
                                        break;
                                    case ColumLienHe._Email:
                                        child.InnerText = info.Email.ToString();
                                        break;
                                    case ColumLienHe._Name:
                                        child.InnerText = info.Name.ToString();
                                        break;
                                    case ColumLienHe._Phone:
                                        child.InnerText = info.Phone.ToString();
                                        break;
                                    case ColumLienHe._PL:                                       
                                        child.InnerText = info.PL.ToString();
                                        break;
                                    case ColumLienHe._Title:
                                        child.InnerText = info.Title;
                                        break;
                                    case ColumLienHe._UrlAdmin:
                                        child.InnerText = info.UrlAdmin;
                                        break;
                                    case ColumLienHe._UrlFronent:
                                        child.InnerText = info.UrlFronent;
                                        break;
                                }
                            }
                        }
                    }

                }
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        //#region ===============Img Provider================
        //public static List<ImageInfo> GetAll_Images(string pathFileXMl)
        //{
        //    try
        //    {
        //        List<ImageInfo> result = new List<ImageInfo>();
        //        ImageInfo ImageInfo;
        //        XmlDocument xmlDoc = new XmlDocument();
        //        xmlDoc.Load(pathFileXMl);
        //        XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
        //        foreach (XmlNode node in nodes)
        //        {
        //            ImageInfo = new ImageInfo();
        //            foreach (XmlNode childNode in node.ChildNodes)
        //            {
        //                if (childNode.NodeType == XmlNodeType.Element)
        //                {
        //                    switch (childNode.Name)
        //                    {
        //                        case ColumImg._Id:
        //                            ImageInfo.Id = Convert.ToInt32(childNode.InnerText);
        //                            break;
        //                        case ColumImg._Img:
        //                            ImageInfo.Img = childNode.InnerText;
        //                            break;
        //                        case ColumImg._New:
        //                            ImageInfo.New = childNode.InnerText;
        //                            break;
        //                        case ColumImg._Parent:
        //                            ImageInfo.Parent = Convert.ToInt32(childNode.InnerText);
        //                            break;
        //                        case ColumImg._Summary:
        //                            ImageInfo.Summary = childNode.InnerText;
        //                            break;
        //                        case ColumImg._Title:
        //                            ImageInfo.Title = childNode.InnerText;
        //                            break;
        //                        case ColumImg._UrlAdmin:
        //                            ImageInfo.UrlAdmin = childNode.InnerText;
        //                            break;
        //                        case ColumImg._UrlFronent:
        //                            ImageInfo.UrlFronent = childNode.InnerText;
        //                            break;                                
        //                    }

        //                }

        //            }
        //            result.Add(ImageInfo);
        //        }
        //        return result;
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //}
        //public static ImageInfo GetImgById(int id, List<ImageInfo> list)
        //{
        //    return list.Single(cat => cat.Id == id);
        //}
        //public static bool Insert_Img(ImageInfo info, string pathFileXML)
        //{
        //    try
        //    {
        //        XmlDocument xmlDoc = new XmlDocument();
        //        xmlDoc.Load(pathFileXML);
        //        int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
        //        info.Id = lastID + 1;
        //        XmlElement ImgElement = xmlDoc.CreateElement("Root");

        //        CreateElement(xmlDoc, ImgElement, ColumImg._Id, info.Id.ToString());
        //        CreateElement(xmlDoc, ImgElement, ColumImg._Img, info.Img.ToString());
        //        CreateElement(xmlDoc, ImgElement, ColumImg._New, info.New.ToString());
        //        CreateElement(xmlDoc, ImgElement, ColumImg._Parent, info.Parent.ToString());
        //        CreateElement(xmlDoc, ImgElement, ColumImg._Summary, info.Summary);
        //        CreateElement(xmlDoc, ImgElement, ColumImg._Title, info.Title.ToString());
        //        CreateElement(xmlDoc, ImgElement, ColumImg._UrlAdmin, info.UrlAdmin.ToString());
        //        CreateElement(xmlDoc, ImgElement, ColumImg._UrlFronent, info.UrlFronent.ToString());
        //        xmlDoc.DocumentElement.AppendChild(ImgElement);
        //        xmlDoc.Save(pathFileXML);
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}


        //public static bool Update_Img(ImageInfo info, string pathFileXML)
        //{
        //    try
        //    {
        //        XmlDocument xmlDoc = new XmlDocument();
        //        xmlDoc.Load(pathFileXML);
        //        foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
        //        {
        //            if (node.FirstChild.InnerText == info.Id.ToString())
        //            {
        //                foreach (XmlNode child in node.ChildNodes)
        //                {

        //                    if (child.NodeType == XmlNodeType.Element)
        //                    {
        //                        switch (child.Name)
        //                        {
        //                            case ColumImg._Id:
        //                                child.InnerText = info.Id.ToString();
        //                                break;
        //                            case ColumImg._Img:
        //                                child.InnerText = info.Img.ToString();
        //                                break;
        //                            case ColumImg._New:
        //                                child.InnerText = info.New.ToString();
        //                                break;
        //                            case ColumImg._Parent:
        //                                child.InnerText = info.Parent.ToString();
        //                                break;
        //                            case ColumImg._Summary:
        //                                child.InnerText = info.Summary.ToString();
        //                                break;
        //                            case ColumImg._Title:
        //                                child.InnerText = info.Title.ToString();
        //                                break;
        //                            case ColumImg._UrlAdmin:
        //                                child.InnerText = info.UrlAdmin.ToString();
        //                                break;
        //                            case ColumImg._UrlFronent:
        //                                child.InnerText = info.UrlFronent;
        //                                break;                                   
        //                        }
        //                    }
        //                }
        //            }

        //        }
        //        xmlDoc.Save(pathFileXML);
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}
        //#endregion

        #region ===============Images Provider================
        public static List<ImageInfo> GetAll_Images(string pathFileXMl)
        {
            try
            {
                List<ImageInfo> result = new List<ImageInfo>();
                ImageInfo ImageInfo;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXMl);
                XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    ImageInfo = new ImageInfo();
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.NodeType == XmlNodeType.Element)
                        {
                            switch (childNode.Name)
                            {
                                case ColumImages._Id:
                                    ImageInfo.Id = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumImages._urlImages:
                                    ImageInfo.urlImages = childNode.InnerText;
                                    break;
                            }

                        }

                    }
                    result.Add(ImageInfo);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        public static ImageInfo GetImagesById(int id, List<ImageInfo> list)
        {
            return list.Single(cat => cat.Id == id);
        }
        public static bool Insert_Images(ImageInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                if(xmlDoc.DocumentElement.LastChild==null)
                {
                    info.Id = 1;
                }
                
                if (xmlDoc.DocumentElement.LastChild != null)
                {
                    int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
                    info.Id = lastID + 1;
                }                
                XmlElement ImgElement = xmlDoc.CreateElement("Images");
                CreateElement(xmlDoc, ImgElement, ColumImages._Id, info.Id.ToString());
                CreateElement(xmlDoc, ImgElement, ColumImages._urlImages, info.urlImages.ToString());
                xmlDoc.DocumentElement.AppendChild(ImgElement);
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region ===============ContentManagement Provider================
        public static List<ContentManagementInfo> GetAll_ContentManagement(string pathFileXMl)
        {
            try
            {
                List<ContentManagementInfo> result = new List<ContentManagementInfo>();
                ContentManagementInfo ContentManagementInfo;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXMl);
                XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    ContentManagementInfo = new ContentManagementInfo();
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.NodeType == XmlNodeType.Element)
                        {
                            switch (childNode.Name)
                            {
                                case ColumContentManagement._Id:
                                    ContentManagementInfo.Id = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumContentManagement._Active:
                                    ContentManagementInfo.Active = childNode.InnerText;
                                    break;
                                case ColumContentManagement._Name:
                                    ContentManagementInfo.Name = childNode.InnerText;
                                    break;
                                case ColumContentManagement._Parent:
                                    ContentManagementInfo.Parent = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumContentManagement._NameEl:
                                    ContentManagementInfo.NameEl = childNode.InnerText;
                                    break;
                                case ColumContentManagement._Priority:
                                    ContentManagementInfo.Priority = Convert.ToInt32(childNode.InnerText);
                                    break;
                                case ColumContentManagement._Url:
                                    ContentManagementInfo.Url = childNode.InnerText;
                                    break;
                                
                            }

                        }

                    }
                    result.Add(ContentManagementInfo);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        public static ContentManagementInfo GetContentManagementById(int id, List<ContentManagementInfo> list)
        {
            return list.Single(cat => cat.Id == id);
        }
        public static bool Insert_ContentManagement(ContentManagementInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                int lastID = Convert.ToInt32(xmlDoc.DocumentElement.LastChild.FirstChild.InnerText);
                info.Id = lastID + 1;
                XmlElement ContentManagementElement = xmlDoc.CreateElement("Root");

                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._Id, info.Id.ToString());
                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._Active, info.Active.ToString());
                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._Name, info.Name.ToString());
                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._Parent, info.Parent.ToString());
                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._NameEl, info.NameEl);
                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._Priority, info.Priority.ToString());
                CreateElement(xmlDoc, ContentManagementElement, ColumContentManagement._Url, info.Url.ToString());
                xmlDoc.DocumentElement.AppendChild(ContentManagementElement);
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }


        public static bool Update_ContentManagement(ContentManagementInfo info, string pathFileXML)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(pathFileXML);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    if (node.FirstChild.InnerText == info.Id.ToString())
                    {
                        foreach (XmlNode child in node.ChildNodes)
                        {

                            if (child.NodeType == XmlNodeType.Element)
                            {
                                switch (child.Name)
                                {
                                    case ColumContentManagement._Id:
                                        child.InnerText = info.Id.ToString();
                                        break;
                                    case ColumContentManagement._Active:
                                        child.InnerText = info.Active.ToString();
                                        break;
                                    case ColumContentManagement._Name:
                                        child.InnerText = info.Name.ToString();
                                        break;
                                    case ColumContentManagement._Parent:
                                        child.InnerText = info.Parent.ToString();
                                        break;
                                    case ColumContentManagement._NameEl:
                                        child.InnerText = info.NameEl.ToString();
                                        break;
                                    case ColumContentManagement._Priority:
                                        child.InnerText = info.Priority.ToString();
                                        break;
                                    case ColumContentManagement._Url:
                                        child.InnerText = info.Url.ToString();
                                        break;
                                }
                            }
                        }
                    }

                }
                xmlDoc.Save(pathFileXML);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}